package com.cnzz.site1255174697.holder;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cnzz.site1255174697.ConstHost;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.model.GoodsTagEntity;
import com.cqyanyu.yanyu.view.XBaseViewHolder;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 首页-分类适配器
 * （促销，特惠）
 */
public class GoodsKindsAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsTagEntity> list;

    public GoodsKindsAdapter(Context context, List<GoodsTagEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public GoodsTagEntity getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_index_kinds, null);
        }

        ImageView ivFq = XBaseViewHolder.get(convertView, R.id.iv_fq);
        TextView tvKindsName = XBaseViewHolder.get(convertView, R.id.tv_kinds_name);
        ImageView ivOne = XBaseViewHolder.get(convertView, R.id.iv_one);
        ImageView ivTwo = XBaseViewHolder.get(convertView, R.id.iv_two);
        ImageView ivThree = XBaseViewHolder.get(convertView, R.id.iv_three);

        ImageView ivPq = XBaseViewHolder.get(convertView, R.id.iv_pp);
        TextView tvKindsPp = XBaseViewHolder.get(convertView, R.id.tv_kinds_pp);
        ImageView ivImgOne = XBaseViewHolder.get(convertView, R.id.iv_img_one);
        ImageView ivImgTwo = XBaseViewHolder.get(convertView, R.id.iv_img_two);
        ImageView ivImgThree = XBaseViewHolder.get(convertView, R.id.iv_img_three);

        GoodsTagEntity goodsTagEntity = list.get(position);
        if (TextUtils.equals(goodsTagEntity.getName(), "促销疯抢")) {
            Picasso.with(context).load(ConstHost.IMG_BASE_URL + goodsTagEntity.getIcon()).into(ivFq);
            tvKindsName.setText(goodsTagEntity.getName());
            try {
                if (goodsTagEntity.getGoods_list() != null && goodsTagEntity.getGoods_list().size() > 0) {
                    Picasso.with(context).load(ConstHost.IMG_BASE_URL + goodsTagEntity.getGoods_list().get(0).getImg()).into(ivOne);
                    Picasso.with(context).load(ConstHost.IMG_BASE_URL + goodsTagEntity.getGoods_list().get(1).getImg()).into(ivTwo);
                    Picasso.with(context).load(ConstHost.IMG_BASE_URL + goodsTagEntity.getGoods_list().get(2).getImg()).into(ivThree);
                }
            } catch (Exception e) {
            }

        } else {
            Picasso.with(context).load(ConstHost.IMG_BASE_URL + goodsTagEntity.getIcon()).into(ivPq);
            tvKindsPp.setText(goodsTagEntity.getName());
            try {
                if (goodsTagEntity.getGoods_list() != null && goodsTagEntity.getGoods_list().size() > 0) {
                    Picasso.with(context).load(ConstHost.IMG_BASE_URL + goodsTagEntity.getGoods_list().get(0).getImg()).into(ivImgOne);
                    Picasso.with(context).load(ConstHost.IMG_BASE_URL + goodsTagEntity.getGoods_list().get(1).getImg()).into(ivImgTwo);
                    Picasso.with(context).load(ConstHost.IMG_BASE_URL + goodsTagEntity.getGoods_list().get(2).getImg()).into(ivImgThree);
                }
            } catch (Exception e) {
            }

        }
        return convertView;
    }
}
